package krt.wid.gslzs.activity;

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Response;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import krt.wid.gslzs.R;
import krt.wid.gslzs.adpater.NewsAdapter;
import krt.wid.gslzs.base.BaseActivity;
import krt.wid.gslzs.base.BaseFragment;
import krt.wid.gslzs.bean.AllNewsBean;
import krt.wid.gslzs.bean.IndexAppealInfo;
import krt.wid.gslzs.bean.NewsBean;
import krt.wid.gslzs.bean.TotalCountBean;
import krt.wid.gslzs.util.GlideImageLoade;
import krt.wid.gslzs.util.UrlUtil;
import krt.wid.http.MHttp;
import krt.wid.http.MResponseListener;
import krt.wid.http.Result;
import krt.wid.util.MToast;

/**
 * @author Marcus
 * @package krt.wid.gslzs.activity
 * @description
 * @time 2018/5/12
 */

public class GovMainFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener,View.OnClickListener {
    RecyclerView mRecyclerView;

    SwipeRefreshLayout swipe;

    NewsAdapter mAdapter;

    private Banner banner;

    private TextView dslCount, dblCount, ybjCount, ywcCount, title, qqbh;

    private RelativeLayout dblRev, dslRev, ybjRev, ywcRev;

    private int pageCount = 0;

    private String entCode = "";

    private LinearLayout zfb_line,qyb_line,fgzx_layout,zqhd_layout,sqpj_layout;

    private RelativeLayout constraim;

    private ImageView person;

    private int Sqid;//诉求id

    public static GovMainFragment newInstance(String entCode) {

        Bundle args = new Bundle();
        GovMainFragment fragment = new GovMainFragment();
        args.putString("entCode", entCode);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_gov_main;
    }

    @Override
    public void initView(View view) {
        if (getArguments() != null) {
            entCode = getArguments().getString("entCode", "");
        }
        Sqid=0;
        swipe = (SwipeRefreshLayout) view.findViewById(R.id.swipe);
        swipe.setColorSchemeResources(R.color.color_3087f8);
        mRecyclerView = view.findViewById(R.id.recycler);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        person=(ImageView)view.findViewById(R.id.person);
        person.setOnClickListener(this);
        mAdapter = new NewsAdapter(null);
        mRecyclerView.setAdapter(mAdapter);
        swipe.setEnabled(false);
//        swipe.setOnRefreshListener(this);
        mAdapter.addHeaderView(initHeaderView());
        mAdapter.setOnLoadMoreListener(this, mRecyclerView);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(mContext, WebActivity.class).putExtra("url", UrlUtil.getUrl("FeiGongZiXunXiangQing") + "?id=" + mAdapter.getData().get(position).getId() + "&goIndex=1"));
            }
        });

    }

    private View initHeaderView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_header, null);
        dslCount = (TextView) view.findViewById(R.id.dslcount);
        dblCount = (TextView) view.findViewById(R.id.dblcount);
        ybjCount = (TextView) view.findViewById(R.id.ybjcount);
        ywcCount = (TextView) view.findViewById(R.id.ywccount);
        dblRev = (RelativeLayout) view.findViewById(R.id.dbl_rel);
        dslRev = (RelativeLayout) view.findViewById(R.id.dsl_rel);
        ybjRev = (RelativeLayout) view.findViewById(R.id.ybj_rel);
        ywcRev = (RelativeLayout) view.findViewById(R.id.ywc_rel);
        title = (TextView) view.findViewById(R.id.title);
        qqbh = (TextView) view.findViewById(R.id.qqbh);
        banner =(Banner) view.findViewById(R.id.banner);
        zfb_line = (LinearLayout) view.findViewById(R.id.zfbline);
        qyb_line = (LinearLayout) view.findViewById(R.id.qy_line);
        fgzx_layout = (LinearLayout) view.findViewById(R.id.fgzx_layout);
        zqhd_layout = (LinearLayout) view.findViewById(R.id.zqhd_layout);
        sqpj_layout = (LinearLayout) view.findViewById(R.id.sqpj_layout);
        constraim = (RelativeLayout) view.findViewById(R.id.constraim);


        dblRev.setOnClickListener(this);
        dslRev.setOnClickListener(this);
        ybjRev.setOnClickListener(this);
        ywcRev.setOnClickListener(this);

        fgzx_layout.setOnClickListener(this);
        zqhd_layout.setOnClickListener(this);
        sqpj_layout.setOnClickListener(this);
        constraim.setOnClickListener(this);
        person.setOnClickListener(this);

        if(entCode.isEmpty()){
            zfb_line.setVisibility(View.VISIBLE);
            qyb_line.setVisibility(View.GONE);
        }else{

            zfb_line.setVisibility(View.GONE);
            qyb_line.setVisibility(View.VISIBLE);
        }

        return view;
    }


    private void initBanner(final List<AllNewsBean.Data1Bean> datalist) {
        //本地图片数据（资源文件）
        List<String> imglist = new ArrayList<>();
        List<String> titleList = new ArrayList<>();
        for (int i=0;i<datalist.size();i++){
            imglist.add(datalist.get(i).getImg());
            titleList.add(datalist.get(i).getTheme());
        }


        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        banner.setOffscreenPageLimit(2);
//        banner.setPageTransformer(false, new DepthPageTransformer());
        banner.setImageLoader(new GlideImageLoade());
        banner.isAutoPlay(true);

        //设置轮播图片点击事件
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                startActivity(new Intent(mContext, WebActivity.class).putExtra("url", UrlUtil.getUrl("FeiGongZiXunXiangQing") + "?id=" + datalist.get(position).getId()+"&goIndex=1"));
            }
        });
        banner.update(imglist, titleList);
    }


    @Override
    public void loadData() {
        getDate(0);


    }

    private void getDate(int page) {
        MHttp.<Result<AllNewsBean>>create(getActivity())
                .setUrl("http://111.75.240.74:13381/non_public_economy/api/ImageDateList")
                .addParams("start", page + "")
                .addParams("length", 10 + "")
                .addParams("accessToken", mSpUtil.getAccessToken())
                .execute(new MResponseListener<Result<AllNewsBean>>() {
                    @Override
                    public void onSuccess(int what, Result<AllNewsBean> listResult) {

                        if (listResult.isSuccess()) {
                            if (swipe.isRefreshing()) swipe.setRefreshing(false);
                            mAdapter.loadMoreComplete();
                            if(listResult.getData().getData2()!=null) {
                                if (mAdapter.getData().isEmpty()) {
                                    mAdapter.setNewData(listResult.getData().getData2());
                                    initLoad(listResult.getData().getData2());
                                } else {
                                    if (pageCount == 0) {
                                        mAdapter.setNewData(listResult.getData().getData2());
                                        initLoad(listResult.getData().getData2());
                                    } else {
                                        mAdapter.addData(listResult.getData().getData2());
                                        initLoad(listResult.getData().getData2());
                                    }
                                }
                            }
                            if(listResult.getData().getData1()!=null){
                                initBanner(listResult.getData().getData1());
                            }else{
                                banner.setVisibility(View.GONE);
                            }


                        } else {
                            MToast.showToast(mContext, listResult.getMsg());
                        }
                    }

                    @Override
                    public void onFailed(int what, Response<Result<AllNewsBean>> response) {
                        if (swipe.isRefreshing()) swipe.setRefreshing(false);
                        mAdapter.setEnableLoadMore(true);
                    }
                });
        if(entCode.isEmpty()){
            MHttp.<Result<List<TotalCountBean>>>create(getActivity())
                    .setUrl("http://111.75.240.74:13381/non_public_economy/api/appealTotalCount")
                    .addParams("accessToken", mSpUtil.getAccessToken())
                    .setMethod(RequestMethod.GET)
                    .execute(new MResponseListener<Result<List<TotalCountBean>>>() {
                        @Override
                        public void onSuccess(int what, Result<List<TotalCountBean>> listResult) {
                            if (listResult.isSuccess()) {
                                if (listResult.getData().size() > 0) {
                                    for (int i = 0; i < listResult.getData().size(); i++) {
                                        switch (listResult.getData().get(i).getCode()) {
                                            case "1": //待受理
                                                dslCount.setText(listResult.getData().get(i).getCount() + "");
                                                break;
                                            case "2": //代办理
                                                dblCount.setText(listResult.getData().get(i).getCount() + "");
                                                break;
                                            case "3": //已办结
                                                ybjCount.setText(listResult.getData().get(i).getCount() + "");
                                                break;
                                            case "4": //已完成
                                                ywcCount.setText(listResult.getData().get(i).getCount() + "");
                                                break;
                                        }
                                    }
                                }
                            }
                        }

                        @Override
                        public void onFailed(int what, Response<Result<List<TotalCountBean>>> response) {

                        }
                    });

            MHttp.<Result<List<IndexAppealInfo>>>create(getActivity())
                    .setUrl("http://111.75.240.74:13381/non_public_economy/api/indexAppeal")
                    .addParams("accessToken",mSpUtil.getAccessToken())
                    .setMethod(RequestMethod.GET)
                    .execute(new MResponseListener<Result<List<IndexAppealInfo>>>() {
                        @Override
                        public void onSuccess(int what, Result<List<IndexAppealInfo>> listResult) {
                            if (listResult.isSuccess()) {
                                if (listResult.getData().size() > 0) {
                                    qqbh.setText("诉求编号:" + listResult.getData().get(0).getAppealNo());
                                    title.setText(listResult.getData().get(0).getStepName());
                                    Sqid = listResult.getData().get(0).getId();
                                }
                            }
                        }

                        @Override
                        public void onFailed(int what, Response<Result<List<IndexAppealInfo>>> response) {

                        }
                    });
        }

    }


    @Override
    public void onRefresh() {
        mAdapter.setEnableLoadMore(false);
        pageCount = 0;
        getDate(pageCount);
    }

    @Override
    public void onLoadMoreRequested() {
        pageCount++;
        getDate(pageCount);
    }

    private void initLoad(List<NewsBean> listResult) {
        if (listResult.size() < 10) {
            mAdapter.loadMoreEnd();
        } else {
            mAdapter.setEnableLoadMore(true);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dsl_rel:
                startActivity(new Intent(mContext, WebActivity.class).putExtra("url", UrlUtil.getUrl("ZhengShangHuDong") + "?code=1&goIndex=1"));
                break;
            case R.id.dbl_rel:
                startActivity(new Intent(mContext, WebActivity.class).putExtra("url", UrlUtil.getUrl("ZhengShangHuDong") + "?code=2&goIndex=1"));
                break;
            case R.id.ybj_rel:
                startActivity(new Intent(mContext, WebActivity.class).putExtra("url", UrlUtil.getUrl("ZhengShangHuDong") + "?code=3&goIndex=1"));
                break;
            case R.id.ywc_rel:
                startActivity(new Intent(mContext, WebActivity.class).putExtra("url", UrlUtil.getUrl("ZhengShangHuDong") + "?code=4&goIndex=1"));
                break;
            case R.id.fgzx_layout:
                startActivity(new Intent(mContext, WebActivity.class).putExtra("url", UrlUtil.getUrl("FeiGongZiXun&goIndex=1")));
                break;
            case R.id.zqhd_layout:
                startActivity(new Intent(mContext, AppealActivity.class));
                break;
            case R.id.sqpj_layout:
                startActivity(new Intent(mContext, WebActivity.class).putExtra("url", UrlUtil.getUrl("SuQiuPingJia&goIndex=1")));
                break;
            case R.id.constraim:
                startActivity(new Intent(mContext, WebActivity.class).putExtra("url", UrlUtil.getUrl("SuQiuXiangQing") + "?id=" + Sqid +"&goIndex=1"));
                break;
            case R.id.person:
                if(entCode.isEmpty()){ //政府版
                    startActivity(new Intent(mContext, WebActivity.class).putExtra("url", UrlUtil.getUrl("ZhengFu-GeRenZhongXin&goIndex=1")));
                }else{ //企业版
                    startActivity(new Intent(mContext, WebActivity.class).putExtra("url", UrlUtil.getUrl("QiYeGeRenZhongXin&goIndex=1")));
                }

                break;

        }
    }
}
